package com.lemon.secondhandplatform.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.Comment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentHolder> {

    private OnCommentItemClickListener onCommentItemClickListener;

    private Context context;
    private List<Comment> data;

    public CommentAdapter(Context context, List<Comment> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public CommentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_comment, parent, false);
        return new CommentHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentHolder holder, int position) {
        Comment comment = data.get(position);
        holder.bind(comment);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    /**
     * 刷新数据
     *
     * @param data
     */
    public void refresh(List<Comment> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    /**
     * 追加数据
     *
     * @param data
     */
    public void append(List<Comment> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void setOnCommentItemClickListener(OnCommentItemClickListener onCommentItemClickListener) {
        this.onCommentItemClickListener = onCommentItemClickListener;
    }

    class CommentHolder extends RecyclerView.ViewHolder {

        private Comment comment;

        //用户头像
        @BindView(R.id.imgAvatar)
        CircleImageView imgAvatar;
        //评论
        @BindView(R.id.tvComment)
        TextView tvComment;
        //楼层
        @BindView(R.id.tvFloor)
        TextView tvFloor;
        //创建时间
        @BindView(R.id.tvCreatedAt)
        TextView tvCreatedAt;


        public CommentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(Comment comment) {
            this.comment = comment;
            Glide.with(context)
                    .load(comment.getOwner().getAvator())
                    .into(imgAvatar);
            if (comment.getReply() != null) {
                tvComment.setText("回复 " + comment.getReply().getNickname() + ": " + comment.getContent());
            } else {
                tvComment.setText(comment.getContent());
            }
            tvFloor.setText("#" + comment.getFloor().toString());
            tvCreatedAt.setText(comment.getCreatedAt());
        }

        /**
         * 点击用户头像查看用户信息
         * @param v
         */
        @OnClick(R.id.imgAvatar)
        public void avator(View v) {
            if (onCommentItemClickListener != null) {
                onCommentItemClickListener.onAvatarClick(v, comment);
            }
        }

        /**
         * 点击评论回复
         * @param v
         */
        @OnClick(R.id.tvComment)
        public void comment(View v) {
            if (onCommentItemClickListener != null) {
                onCommentItemClickListener.onCommentClick(v, comment);
            }
        }

    }

    public interface OnCommentItemClickListener {

        void onAvatarClick(View v, Comment comment);

        void onCommentClick(View v, Comment comment);

    }
}
